package com.example.myapplication;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

public class SwipeItemTouchHelperCallBack extends ItemTouchHelper.Callback {

  private NoteArrayListAdapter adapter;
  public SwipeItemTouchHelperCallBack(NoteArrayListAdapter adapter) {
    this.adapter = adapter;
  }

  @Override public int getMovementFlags(@NonNull RecyclerView recyclerView,
      @NonNull RecyclerView.ViewHolder viewHolder) {
    int dragFlags = 0;
    int swipeFlags = 0; // 禁用滑动操作
    return makeMovementFlags(dragFlags, swipeFlags);
  }
  //处理滑动事件

  @Override public boolean onMove(@NonNull RecyclerView recyclerView,
      @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    return false;
  }


  @Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
    //显示更多图片或执行相应操作
    adapter.ShowAction(viewHolder.getAdapterPosition());

  }

  //@Override public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
  //    @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
  //    boolean isCurrentlyActive) {
  //  if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
  //    //在这里绘制操作按钮的视图
  //    //adapter.DrawActions(c, viewHolder, dX);
  //    // 获取Item的视图
  //    View itemView = viewHolder.itemView;
  //
  //    // 绘制删除按钮的背景色
  //    Paint paint = new Paint();
  //    paint.setColor(Color.RED);
  //    RectF background = new RectF(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(),
  //        itemView.getBottom());
  //    c.drawRect(background, paint);
  //
  //    // 绘制删除按钮的图标
  //    Drawable deleteIcon =
  //        ContextCompat.getDrawable(recyclerView.getContext(), R.drawable.baseline_delete_24);
  //    if (deleteIcon != null) {
  //      int deleteIconMargin = (itemView.getHeight() - deleteIcon.getIntrinsicHeight()) / 2;
  //      int deleteIconTop =
  //          itemView.getTop() + (itemView.getHeight() - deleteIcon.getIntrinsicHeight()) / 2;
  //      int deleteIconBottom = deleteIconTop + deleteIcon.getIntrinsicHeight();
  //      int deleteIconLeft =
  //          itemView.getRight() - deleteIconMargin - deleteIcon.getIntrinsicWidth();
  //      int deleteIconRight = itemView.getRight() - deleteIconMargin;
  //      deleteIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom);
  //      deleteIcon.draw(c);
  //    } else {
  //      super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
  //    }
  //  }
  //}
}
